package com.shu.mmall.service.impl;

import com.shu.mmall.common.Result;
import com.shu.mmall.dao.ProductMapper;
import com.shu.mmall.pojo.Product;
import com.shu.mmall.service.IProductService;
import com.shu.mmall.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shujinhao
 */
@Service("productService")
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Result saveOrUpdateProduct(Product product){
        if (EmptyUtil.isNotNull(product)){
            if (EmptyUtil.isNotBlank(product.getSubImages())){
                String[] subImagArray=product.getSubImages().split(",");
                if (subImagArray.length>0){
                    product.setMainImage(subImagArray[0]);
                }
                if (product.getId()!=null ){
                    int rowCount=productMapper.updateByPrimaryKey(product);
                    if (rowCount>0){
                        return Result.createBySuccess("更新产品成功");
                    }
                    return Result.createByErrorMessage("更新产品失败");
                }
                else {
                    int rowCount=productMapper.insert(product);
                    if (rowCount>0){
                        return Result.createBySuccess("新增产品成功");
                    }
                    return Result.createByErrorMessage("新增产品失败");
                }
            }
        }
        return Result.createByErrorMessage("新增或更新产品参数不正确");
    }
}
